package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.UserDo;
import com.weijianhuawen.wjblog.common.domain.dos.UserRoleDo;

import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-17 17:37
 */

public interface UserRoleMapper extends BaseMapper<UserRoleDo> {
    //根据用户名进行查询
    default List<UserRoleDo> selectUserRoleByUsername(String username) {
        LambdaQueryWrapper<UserRoleDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleDo::getUsername, username);

        return selectList(wrapper);
    }

    // 分页获取用户角色信息
    default Page<UserRoleDo> selectUserRolePageList(Long current, Long size, String username) {
        // 创建分页
        Page<UserRoleDo> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<UserRoleDo> wrapper = Wrappers.<UserRoleDo>lambdaQuery()
                .like(StringUtils.isNotBlank(username), UserRoleDo::getUsername, username)
                .orderByDesc(UserRoleDo::getCreateTime);

        // 进行分页查询
        return selectPage(page, wrapper);
    }

    // 根据用户名与ROLE查询
    default UserRoleDo selectUserRoleByUsernameAndRole(String username, String role) {
        return selectOne(Wrappers.<UserRoleDo>lambdaQuery()
                .eq(StringUtils.isNotBlank(username), UserRoleDo::getUsername, username)
                .eq(StringUtils.isNotBlank(role), UserRoleDo::getRole, role)
                .last("limit 1"));
    }
}
